package com.world.listener;

import com.dtflys.forest.Forest;
import com.dtflys.forest.http.HttpStatus;
import com.world.cache.DefaultMemoryCacheOperator;
import com.world.constants.Constants;
import com.world.request.RealmClient;
import com.world.request.dto.PageResult;
import com.world.request.dto.RealmPageRequest;
import com.world.request.dto.RealmVO;
import com.world.request.dto.ResponseData;
import com.world.table.RealmTableModel;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.List;
import java.util.Optional;

/**
 * 境界管理鼠标点击监听
 *
 * @author WangYX
 * @version 1.0.0
 * @date 2023/11/30 9:31
 */
public class RealmMouseListener implements MouseListener {

    private final JLayeredPane contentArea;

    private final JPanel panel;

    private Long current = 1L;

    private Long size = 10L;

    private JTable table;

    private RealmTableModel realmTableModel = new RealmTableModel();

    /**
     * 页脚
     */
    private JPanel footerPanel;

    public RealmMouseListener(JLayeredPane contentArea, JPanel panel) {
        this.contentArea = contentArea;
        this.panel = panel;
    }


    @Override
    public void mouseClicked(MouseEvent e) {
        remove();
        repaint();
        data();
    }

    @Override
    public void mousePressed(MouseEvent e) {

    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {
        Color color = new Color(236, 239, 255);
        panel.setBackground(color);
    }

    @Override
    public void mouseExited(MouseEvent e) {
        Color color = new Color(251, 248, 255);
        panel.setBackground(color);
    }

    /**
     * 移除所有组件
     */
    public void remove() {
        contentArea.removeAll();
    }

    /**
     * 重新绘制
     */
    private void repaint() {
        contentArea.repaint();
    }

    /**
     * 添加数据
     */
    public void data() {
        PageResult<RealmVO> data = getData(current, size);
        Optional.ofNullable(data).ifPresent(i -> {
            contentArea.add(body(i.getRecords()), JLayeredPane.MODAL_LAYER);
            contentArea.add(footer(data.getCurrent(), data.getSize(), data.getTotal()), JLayeredPane.MODAL_LAYER);
        });
    }

    /**
     * 页面
     */
    private JPanel body(List<RealmVO> records) {
        realmTableModel.setList(records);
        //页面
        JPanel panel = new JPanel();
        panel.setBounds(5, 5, 800, 330);
        panel.setLayout(new BorderLayout());

        table = new JTable(realmTableModel);
        table.setRowHeight(30);
        //禁止行选择
        table.setRowSelectionAllowed(false);

        //列宽
        table.getColumnModel().getColumn(0).setPreferredWidth(20);
        table.getColumnModel().getColumn(1).setPreferredWidth(30);
        table.getColumnModel().getColumn(2).setPreferredWidth(40);


        JScrollPane jsp = new JScrollPane(table);

        panel.add(jsp, BorderLayout.CENTER);
        return panel;
    }


    /**
     * 页脚
     */
    private JPanel footer(Long current, Long size, Long total) {
        footerPanel = new JPanel();
        footerPanel.setLayout(null);
        footerPanel.setBounds(5, 340, 800, 150);

        Font font = new Font("微软雅黑", Font.PLAIN, 16);

        JLabel currentLabel = new JLabel("当前页：" + current + " ");
        currentLabel.setFont(font);
        currentLabel.setBounds(395, 5, 100, 50);
        footerPanel.add(currentLabel);

        //页面
        long pageNum = (total + size - 1) / size;

        JLabel totalLabel = new JLabel("共 " + pageNum + " 页");
        totalLabel.setFont(font);
        totalLabel.setBounds(495, 5, 90, 50);
        footerPanel.add(totalLabel);

        JButton preButton = new JButton("上一页");
        preButton.setFont(font);
        preButton.setBounds(595, 15, 90, 30);
        preButton.setEnabled(current != 1);
        preButton.addActionListener(new PagingActionListener(current - 1L, size));
        footerPanel.add(preButton);

        JButton nextButton = new JButton("下一页");
        nextButton.setFont(font);
        nextButton.setBounds(695, 15, 90, 30);
        nextButton.setEnabled(current != pageNum);
        nextButton.addActionListener(new PagingActionListener(current + 1L, size));
        footerPanel.add(nextButton);

        return footerPanel;
    }

    /**
     * 获取数据
     *
     * @param current 页码
     * @param size    每页大小
     */
    private PageResult<RealmVO> getData(Long current, Long size) {
        RealmClient client = Forest.client(RealmClient.class);
        RealmPageRequest request = new RealmPageRequest();
        request.setCurrent(current);
        request.setSize(size);
        ResponseData<PageResult<RealmVO>> page = client.page(DefaultMemoryCacheOperator.get(Constants.TOKEN).toString(), request);
        if (page.getCode().equals(HttpStatus.OK)) {
            return page.getData();
        } else {
            JOptionPane.showMessageDialog(null, "获取境界信息失败！！");
            return null;
        }
    }

    /**
     * 翻页监听
     *
     * @author WangYX
     * @version 1.0.0
     * @date 2023/11/30 14:15
     */
    private class PagingActionListener implements ActionListener {

        private Long current;

        private Long size;


        public PagingActionListener(Long current, Long size) {
            this.current = current;
            this.size = size;
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            PageResult<RealmVO> data = getData(current, size);
            Optional.ofNullable(data).ifPresent(i -> {
                //表格数据
                realmTableModel.setList(data.getRecords());
                table.repaint();

                //重绘页脚
                contentArea.remove(footerPanel);
                contentArea.repaint();
                contentArea.add(footer(data.getCurrent(), data.getSize(), data.getTotal()), JLayeredPane.MODAL_LAYER);
            });
        }
    }


}
